I am building a spring + hibernate application. I'm getting a 404 error, when I run the project. The compiler does not show any error messages, it indicates that the application server (tomcat 7) loads the servlet, but does not display the root page indicated in the controller class.
Help please......
Config.....
`
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.0.xsd">
<context:property-placeholder location="classpath:resources/database.properties" />
<context:component-scan base-package="langS.com" />
<tx:annotation-driven transaction-manager="hibernateTransactionManager" />
<bean id="jspViewResolver"
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="viewClass"
value="org.springframework.web.servlet.view.JstlView" />
<property name="prefix" value="/WEB-INF/views/" />
<property name="suffix" value=".jsp" />
</bean>
<bean id="dataSource"
class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="${database.driver}" />
<property name="url" value="${database.url}" />
<property name="username" value="${database.user}" />
<property name="password" value="${database.password}" />
</bean>
<bean id="sessionFactory"
class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="packagesToScan">
<list>
<value>com.langS.model.Employee</value>
<value>com.langS.EmployeeBean</value>
</list>
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">${hibernate.dialect}</prop>
<prop key="hibernate.show_sql">${hibernate.show_sql}</prop>
<prop key="hibernate.hbm2ddl.auto">${hibernate.hbm2ddl.auto}</prop>
</props>
</property>
</bean>
<bean id="hibernateTransactionManager"
class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory" />
</bean>
</beans>`
Controller
#Controller
public class NewController {
#Autowired
private EmployeeService employeeService;
#RequestMapping(value = "/index", method = RequestMethod.GET)
public String welcomeHome(){
return "index";
}
#RequestMapping(value = "/addEmployee", method = RequestMethod.GET)
public String addEmployeeRecord(){
return "addEmployee";
}
#RequestMapping(value = "/employeeList", method = RequestMethod.GET)
public String listEmployeesPage(){
return "employeesList";
}
}
web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://java.sun.com/xml/ns/javaee"
xsi:schemalocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
<servlet>
<servlet-name>sdnext</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet
</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/config/dispatcher-servlet.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>sdnext</servlet-name>
<url-pattern>*.html</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
</welcome-file-list>
</web-app>
404 means, that your tomcat server is getting the request, but does not now how to process the given request path.
In other words you have a bug in your URL or your configuration does not what you want it to do.
We need your configuration and desired URL to help you.
The problem is that you map the dispatcher servlet only to *.html, but you map you Controllers without using the .html suffix.
To make it work, either map you controller differently, ie :
#RequestMapping(value = "/index.html", method = RequestMethod.GET)
public String welcomeHome(){
return "index";
}
OR (and what I would do) :
Map you dispatcher servlet to all requests in web.xml :
<servlet-mapping>
<servlet-name>sdnext</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
Then if you want to call your application with a url looking like this :
http://somehost:someport/YourApplication/
Then you'll need to map some controller method like this :
#RequestMapping(value = {"/", "/index"}, method = RequestMethod.GET)
public String home() {
// your code here
}
This way, calling http://somehost:someport/YourApplication/ OR http://somehost:someport/YourApplication/index will result in the same controller method being called.
Related
I already searching about my error but I got nothing. still error in my code so i have to post my question here.
I got an error when I run my project in eclipse with tomcat server.
I already finish the project in Spring MVC 4 + Hibernate with MongoDB. now I have to switch into Hibernate with MySQL, I am starting the project in eclipse. but when I complete the configuration of spring with hibernate its shows error.
error and structure of project are following below:
enter image description here
web.xml
<display-name>Archetype Created Web Application</display-name>
<context-param>
<param-name>ApplicationContext</param-name>
<param-value>/WEB-INF/applicationContext.xml</param-value>
</context-param>
<servlet>
<servlet-name>DMS_MySQL</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet
</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>DMS_MySQL</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
<servlet-mapping>
<servlet-name>default</servlet-name>
<url-pattern>/assets/*</url-pattern>
</servlet-mapping>
<error-page>
<error-code>404</error-code>
<location>/pages/error/error404.jsp</location>
</error-page>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<listener>
<listener-class>org.springframework.web.context.request.RequestContextListener
</listener-class>
</listener>
</web-app>
SpringMVC-servlet.xml
<?xml version="1.0" encoding="UTF-8"?
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:context="http://www.springframework.org/schema/context" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation=" //urls//">
<bean id="tilesConfigurer" class="org.springframework.web.servlet.view.tiles3.TilesConfigurer">
<property name="definitions">
<list>
<value>/WEB-INF/tiles.xml</value>
</list>
</property>
</bean>
<bean id="tilesviewResolver" class="org.springframework.web.servlet.view.UrlBasedViewResolver">
<property name="viewClass" value="org.springframework.web.servlet.view.tiles3.TilesView" />
<property name="order" value="2"></property>
</bean>
<bean id="jstlviewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="viewClass" value="org.springframework.web.servlet.view.JstlView" />
<property name="prefix" value="/pages/" />
<property name="suffix" value=".jsp" />
<property name="order" value="3"></property>
</bean>
</beans>
applicationContext.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">
<!-- Enable autowire -->
<context:annotation-config />
<context:component-scan base-package="com.cs" />
<mvc:annotation-driven />
<mvc:resources mapping="/resources/**" location="/resources/" />
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver" />
<property name="url" value="jdbc:mysql://localhost:3306/dms" />
<property name="username" value="root" />
<property name="password" value="root" />
</bean>
<!-- Session Factory Declaration -->
<bean id="sessionFactory"
class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="packagesToScan" value="com.cs.bean" />
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
<prop key="hibernate.show_sql">true</prop>
<prop key="hibernate.enable_lazy_load_no_trans">true</prop>
<prop key="hibernate.default_schema">test</prop>
<prop key="format_sql">true</prop>
<prop key="use_sql_comments">true</prop>
<prop key="hibernate.hbm2ddl.auto">update</prop>
</props>
</property>
</bean>
<tx:annotation-driven transaction-manager="transactionManager" />
<bean id="transactionManager"
class="org.springframework.orm.hibernate4.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory" />
</bean>
<import resource="/WEB-INF/SpringMVC-servlet.xml"/>
</beans>
LoginController.java
package com.cs.controller;
public class LoginController {
#Autowired
private UserService userService;
#RequestMapping(value="/login")
public String execute(HttpSession session, Model model) throws Exception {
model.addAttribute("user", new UserBean());
UserBean tempUser = (UserBean) session.getAttribute("USER");
if (tempUser != null) {
return "redirect:/home";
}
return "logn";
}
#RequestMapping(value="/performLogin", method = RequestMethod.POST)
public String loginProcess(HttpSession session, Model model, #ModelAttribute("user") UserBean user,
BindingResult result) throws Exception
{
UserBean tempUser = (UserBean) session.getAttribute("USER");
if(tempUser != null)
{
return "redirect:/home";
}
if(user == null)
{
model.addAttribute("user", new UserBean());
return "login";
}
if(StringUtils.isBlank(user.getName()))
{
result.rejectValue("name", "error.required.username");
}
if(StringUtils.isBlank(user.getPassword()))
{
result.rejectValue("password", "error.required.password");
}
if(!result.hasErrors())
{
tempUser = userService.findByCredential(user);
if(tempUser != null)
{
if (tempUser.getRole().equals(RoleEnum.ADMIN)) {
session.setAttribute("USER", tempUser);
return "redirect:/home";
} else if (tempUser.getRole().equals(RoleEnum.CASHIER)) {
session.setAttribute("USER", tempUser);
return "redirect:/cHome";
}
}
else
{
result.reject("error.valid.usernamePassword");
}
}
user.setMobile("");
model.addAttribute("user", user);
return "login";
}
}
Thank you in advance.
Try http://localhost:8080/DMS_MySQL/login.
Also try changing your login controller as:
#RequestMapping(value = {"/login", "/"}, method = RequestMethod.GET)
public String execute(HttpSession session, Model model) throws Exception {
model.addAttribute("user", new UserBean());
UserBean tempUser = (UserBean) session.getAttribute("USER");
if (tempUser != null) {
return "redirect:/home";
}
return "login";//typo in your code
}
I assume you have defined mapping for login in tiles configuration.
here i find this sentence
<param-value>/WEB-INF/applicationContext.xml</param-value>
and where is your applicationContext.xml
I am trying to inject annotated by #Repository class in #ServerEndpoint class. But when i am trying to call repository method it is return null. Another injected bean from this package is working fine.
#ApplicationScoped
#ServerEndpoint("/WebsocketHome/actions")
public class WebSocketServer {
private static final Logger LOG = Logger.getLogger(WebSocketServer.class);
#Inject
private SessionHandler sessionHandler;
#Inject
private PlaceRepository placeRepository;
#OnMessage
public void handleMessage(String message, Session session) {
JSONParser jsonParser = new JSONParser();
try {
JSONObject jsonObject = (JSONObject) jsonParser.parse(message);
if ("getRooms".equals(jsonObject.get("action"))) {
List<Place> places = this.placeRepository.getAllPlaces(); //error is here
}
} catch (ParseException e) {
LOG.error(e.getMessage());
}
.....
This is repository class:
#Repository
#Transactional
public class PlaceRepository {
#Autowired
private SessionFactory sessionFactory;
#SuppressWarnings("unchecked")
public List<Place> getAllPlaces() {
return this.sessionFactory.getCurrentSession().createQuery("from Place place").list();
}
}
web.xml:
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://java.sun.com/xml/ns/javaee"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
version="3.0">
<display-name>Archetype Created Web Application</display-name>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/application-context.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<servlet>
<servlet-name>mvc-dispatcher</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>mvc-dispatcher</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
<filter>
<filter-name>springSecurityFilterChain</filter-name>
<filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>
</filter>
<filter-mapping>
<filter-name>springSecurityFilterChain</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
</web-app>
app-context.xml:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
<context:property-placeholder location="classpath:application.properties" system-properties-mode="ENVIRONMENT"/>
<context:component-scan base-package="com.hms.repository"/>
<context:component-scan base-package="com.hms.utils"/>
<tx:annotation-driven transaction-manager="txManager"/>
<import resource="security-context.xml"/>
<bean id="txManager" class="org.springframework.orm.hibernate5.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory" />
</bean>
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="${db.driverClassName}" />
<property name="url" value="${db.url}" />
<property name="username" value="${db.username}" />
<property name="password" value="${db.password}" />
<!--
<property name="initialSize" value="5" />
<property name="maxActive" value="10" />
<property name="maxIdle" value="5" />
<property name="minIdle" value="2" />
-->
</bean>
<bean id="sessionFactory"
class="org.springframework.orm.hibernate5.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="configLocation" value="classpath:hibernate.cfg.xml"/>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">${db.dialect}</prop>
<prop key="hibernate.show_sql">true</prop>
<prop key="hbm2ddl.auto">${db.hbm2ddl.auto}</prop>
</props>
</property>
</bean>
</beans>
Error appears only on calling of #OnMessage method.
All junit #Tests of repository class returns fine results.
What is wrong in my code?
Sorry for my english.
UPD:
It seems, that problem is in SessionFactory dependency because test method, that i add in #Repository:
public String testWS() {
return "Test is ok!";
}
returns fine result.
So, looks like i found the right way. I found it in Spring websocket documentation.
There are some steps what i did:
I put in type-level annotation:
#ServerEndpoint(value = "/WebsocketHome/actions", configurator = SpringConfigurator.class)
I added #Service and #Controller to my websocket classes and "context:component-scan" path to my app-context.xml file so that Spring can find corresponding beans.
I added the "spring-websocket" dependency in my pom.xml (i use maven)
Maybe it is not the right way, but it is works fine for me.
#Today jan 31' 2022 Just Add "spring-websocket" dependency
This question already has answers here:
Why does Spring MVC respond with a 404 and report "No mapping found for HTTP request with URI [...] in DispatcherServlet"?
(13 answers)
Closed 6 years ago.
I tried all the trails posted in internet mainly in stackoverflow but coudn't find the exact solution of my application problem.
Please don't mark this as duplicate because warning might be the duplicate but version or something else is causing which is not duplicate here i.e the same answers are no working for me
Warning i'm getting:
WARN : org.springframework.web.servlet.PageNotFound - No mapping found for HTTP request with URI in DispatcherServlet with name 'appServlet'
Code of my application:
web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
<!-- The definition of the Root Spring Container shared by all Servlets and Filters -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/spring/root-context.xml</param-value>
</context-param>
<!-- Creates the Spring Container shared by all Servlets and Filters -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!-- Processes application requests -->
<servlet>
<servlet-name>appServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/spring/appServlet/servlet-context.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>appServlet</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>
servlet-context.xml
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-4.0.xsd">
<!-- DispatcherServlet Context: defines this servlet's request-processing
infrastructure -->
<!-- Enables the Spring MVC #Controller programming model -->
<!-- <annotation-driven /> -->
<!-- <context:annotation-config/> -->
<mvc:annotation-driven />
<!-- Handles HTTP GET requests for /resources/** by efficiently serving
up static resources in the ${webappRoot}/resources directory -->
<!-- <resources mapping="/resources/**" location="/resources/" /> -->
<!-- Resolves views selected for rendering by #Controllers to .jsp resources
in the /WEB-INF/views directory -->
<bean
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/views/" />
<property name="suffix" value=".jsp" />
</bean>
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"
destroy-method="close">
<property name="driverClassName" value="com.mysql.jdbc.Driver" />
<property name="url"
value="jdbc:mysql://localhost:3306/TestDB" />
<property name="username" value="root" />
<property name="password" value="root" />
</bean>
<!-- Hibernate 4 SessionFactory Bean definition -->
<bean id="hibernate4AnnotatedSessionFactory"
class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="annotatedClasses">
<list>
<value>com.mahender.web.model.Person</value>
</list>
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect
</prop>
<prop key="hibernate.show_sql">true</prop>
</props>
</property>
</bean>
<bean id="personDAO" class="com.mahender.web.DAO.PersonDAOImpl">
<property name="sessionFactory" ref="hibernate4AnnotatedSessionFactory" />
</bean>
<bean id="personService" class="com.mahender.web.service.PersonServiceImpl">
<property name="personDAO" ref="personDAO"></property>
</bean>
<context:component-scan base-package="com.mahender.web" />
<tx:annotation-driven transaction-manager="transactionManager"/>
<bean id="transactionManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager">
<property name="sessionFactory" ref="hibernate4AnnotatedSessionFactory" />
</bean>
Snippet of my Controller(not all the code provided i think this is enough to find the problem)
#RequestMapping(value = "/app")
#Controller
public class PersonController {
private PersonService personService;
#Autowired(required=true)
#Qualifier(value="personService")
public void setPersonService(PersonService ps){
this.personService = ps;
}
#RequestMapping(value = "/persons", method = RequestMethod.GET)
public String listPersons(Model model) {
model.addAttribute("person", new Person());
model.addAttribute("listPersons", this.personService.listPersons());
return "person";
}
and the jsp snippet of code is:
person.jsp
<c:url var="addAction" value="/person/add" ></c:url>
<form:form action="${addAction}" commandName="person">
You need to add this on your code.
model.setViewName("person");
model.addAttribute("person", new Person());
and also make sure your .jsp file is inside /views folder
Your requested action is not mapped properly in controller try to changed it to app/person/add
I try to get all avaliable groups from db when i load start page. Btu I can't set correct RequestMapping for home page and when i load home page it does load any data to JSP page. I try to set it as
#RequestMapping(value = "/", method = RequestMethod.GET)
public ModelAndView newStudentForm() {
ModelAndView mav = new ModelAndView("");
mav.getModelMap().put("allGroups", gss.selectAllGroups()); // add all available groups
return mav;
}
but i'm not sure that i set correct 'value = "/"' and name in ModelAndView mav = new ModelAndView("");
Here's my xml file with all settings:
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
id="WebApp_ID"
version="3.0">
<display-name>IRSystem</display-name>
<!-- The start page -->
<welcome-file-list>
<welcome-file>/WEB-INF/jsp/index.jsp</welcome-file>
</welcome-file-list>
<servlet>
<servlet-name>IRSystemServlet</servlet-name>
<servlet-class>
org.springframework.web.servlet.DispatcherServlet
</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/spring/IRSystemServlet-servlet.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>IRSystemServlet</servlet-name>
<url-pattern>*.html</url-pattern>
</servlet-mapping>
</web-app>
IRSystemServlet-servlet.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.2.xsd">
<!-- Set packages to scan for necessary components-->
<context:component-scan base-package="org.irs.controllers"/>
<context:component-scan base-package="org.irs.service"/>
<context:component-scan base-package="org.irs.dao"/>
<context:component-scan base-package="org.irs.entitis"/>
<bean id="viewResolver"
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="viewClass"
value="org.springframework.web.servlet.view.JstlView" />
<property name="prefix" value="/WEB-INF/jsp/" />
<property name="suffix" value=".jsp" />
</bean>
<!-- JDBC DataSource -->
<bean id="dataSource"
class="org.apache.commons.dbcp.BasicDataSource"
destroy-method="close">
<property name="driverClassName" value="oracle.jdbc.driver.OracleDriver"/>
<property name="url" value="jdbc:oracle:thin:VLAD-PC/#localhost:1521:orcl"/>
<property name="username" value="VLAD"/>
<property name="password" value="admin"/>
</bean>
<!-- Hibernate session -->
<bean id="sessionFactory"
class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSource"/>
<property name="hibernateProperties">
<value>hibernate.dialect=org.hibernate.dialect.OracleDialect</value>
</property>
<property name="packagesToScan" value="org.irs.entities"/>
</bean>
<tx:annotation-driven transaction-manager="transactionManager"/>
<!-- set response encoding UTF-8 -->
<bean class = "org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter">
<property name="messageConverters">
<array>
<bean class = "org.springframework.http.converter.StringHttpMessageConverter">
<property name="supportedMediaTypes" value = "text/plain;charset=UTF-8" />
</bean>
</array>
</property>
</bean>
<mvc:annotation-driven/>
<!-- Hibernate transaction -->
<bean id="transactionManager"
class="org.springframework.orm.hibernate4.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory" />
</bean>
How should i do it to get all data i need to JSP page when i Load it?
You have set your home page in ModelAndView object something like this
#RequestMapping(value = "/", method = RequestMethod.GET)
public ModelAndView newStudentForm() {
ModelAndView mav = new ModelAndView("index");
mav.getModelMap().put("allGroups", gss.selectAllGroups()); // add all available groups
return mav;
}
you don't need to have welcome page as the moment you hit / it will call the above method.
delete this code
<welcome-file-list>
<welcome-file>/WEB-INF/jsp/index.jsp</welcome-file>
</welcome-file-list>
you have to return the view name in your controller.
since you configured the InternalResourceViewResolver with prefix /WEB-INF/jsp/ and suffix .jsp you should return index to get /WEB-INF/jsp/index.jsp as view.
`ModelAndView mav = new ModelAndView("index");
So I know there are dozens of posts similar to this, but unfortunately none of those have helped me. I am simply trying to get a demo Spring MVC project up and running. I am trying to run a template project provided by Heroku (https://devcenter.heroku.com/articles/getting-started-with-heroku-eclipse). I have tried many combinations of settings to no avail. Here are the default settings:
web.xml:
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
id="WebApp_ID" version="2.5">
<display-name>Spring-Hibernate-Template</display-name>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
<servlet>
<servlet-name>spring</servlet-name>
<servlet-class>
org.springframework.web.servlet.DispatcherServlet
</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>spring</servlet-name>
<url-pattern>/people/*</url-pattern>
</servlet-mapping>
</web-app>
applicationContext.xml:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:jdbc="http://www.springframework.org/schema/jdbc"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd">
<context:annotation-config />
<context:component-scan base-package="com.example" />
<mvc:annotation-driven/>
<bean id="jspViewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="viewClass" value="org.springframework.web.servlet.view.JstlView" />
<property name="prefix" value="/WEB-INF/jsp/" />
<property name="suffix" value=".jsp" />
</bean>
<tx:annotation-driven />
<bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
<property name="entityManagerFactory" ref="entityManagerFactory" />
<property name="dataSource" ref="dataSource"/>
</bean>
<beans profile="default">
<jdbc:embedded-database id="dataSource"/>
<bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
<property name="dataSource" ref="dataSource"/>
<property name="jpaVendorAdapter">
<bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter"/>
</property>
<property name="jpaProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.HSQLDialect</prop>
<prop key="hibernate.hbm2ddl.auto">create</prop>
</props>
</property>
</bean>
</beans>
<beans profile="prod">
<bean class="java.net.URI" id="dbUrl">
<constructor-arg value="#{systemEnvironment['DATABASE_URL']}"/>
</bean>
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
<property name="url" value="#{ 'jdbc:postgresql://' + #dbUrl.getHost() + ':' + #dbUrl.getPort() + #dbUrl.getPath() }"/>
<property name="username" value="#{ #dbUrl.getUserInfo().split(':')[0] }"/>
<property name="password" value="#{ #dbUrl.getUserInfo().split(':')[1] }"/>
</bean>
<bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
<property name="dataSource" ref="dataSource"/>
<property name="jpaVendorAdapter">
<bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter"/>
</property>
<property name="jpaProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.PostgreSQLDialect</prop>
<prop key="hibernate.show_sql">true</prop>
<!-- change this to 'verify' before running as a production app -->
<prop key="hibernate.hbm2ddl.auto">update</prop>
</props>
</property>
</bean>
</beans>
</beans>
PersonController.java:
#Controller
public class PersonController {
#Autowired
private PersonService personService;
#RequestMapping("/")
public String listPeople(Map<String, Object> map) {
map.put("person", new Person());
map.put("peopleList", personService.listPeople());
return "people";
}
#RequestMapping(value = "/add", method = RequestMethod.POST)
public String addPerson(#ModelAttribute("person") Person person, BindingResult result) {
personService.addPerson(person);
return "redirect:/people/";
}
#RequestMapping("/delete/{personId}")
public String deletePerson(#PathVariable("personId") Integer personId) {
personService.removePerson(personId);
return "redirect:/people/";
}
}
And I have a 'people.jsp' file at webapp/WEB-INF/jsp/people/jsp
My server.xml context element for the Tomcat server looks like:
<Context docBase="facultypublicationsdb" path="/facultypublicationsdb" reloadable="true" source="org.eclipse.jst.jee.server:facultypublicationsdb"/></Host>
Every time I run this on Tomcat at (http://localhost:8080/facultypublicationsdb/), I get the following:
HTTP Status 404 - /facultypublicationsdb/
type Status report
message /facultypublicationsdb/
description The requested resource (/facultypublicationsdb/) is not available.
Apache Tomcat/7.0.21
I am running this on Ubuntu through eclipse. I notice that no .war is being copied over to the /usr/share/tomcat7/webapps directory. Is that supposed to occur?
Any ideas?
Try checking two things.
First change your web.xml file to map the dispatcher servlet to /. This causes the dispatcher servlet to be used when no other mappings are found for a request, instead of for every request. This is important if you have resources such as CSS and Javascript.
<servlet-mapping>
<servlet-name>spring</servlet-name>
<url-pattern>/people/*</url-pattern>
</servlet-mapping>
Next, if using Eclipse, open your projects properties (Click project in project, explorer Alt+Enter). Then go to deployment assembly. Make sure that all of your projects resources are included here, especially any maven dependencies. If you notice that dependencies are missing click the add button and select them.
You may also want to ensure that your controllers are being picked up by component scanning. Ensure that your controllers are in the com.example package.
I would think you need to specify method = RequestMethod.GET in listPeople method. There could be many small things that could cause 404 error. Could you upload this code on GitHub and I will look into it.