I have a web app which has JSPs as a view. But now I am trying to implement a RESTful in the same web app. I created a new controller, where will be my RESTful(I am not sure if it makes sense yet).
I have found some examples by internet and I have been trying to make my RESTful following these examples.
When I access my RESTful URL it return me 404 Not Found. I took a look at in some questions similars here in stackoverflow and tried to change some XML configurations, but it does not works, so far.
When I access:
URL: localhost:8080/restfultest/rest/suggestions/11
returns me: 404.
When I access:
URL: localhost:8080/restfultest/loginForm
Returns me the correct JSP page.
I think, maybe I need to set up the XML to be compatible with both kinds, RESTful and a common web app, but I am not sure, and I do not know exactly how to do it.
Thanks and follows my codes:
My web.xml
<servlet>
<servlet-name>springmvc</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/spring-context.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>springmvc</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
spring-context.xml
<context:component-scan base-package="com.restfultest." />
<mvc:annotation-driven />
<bean class="org.springframework.web.servlet.view.json.MappingJacksonJsonView">
<property name="contentType" value="application/json"/>
</bean>
<bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter">
<property name="messageConverters">
<util:list id="beanList">
<ref bean="jsonMessageConverter"/>
</util:list>
</property>
</bean>
<bean id="jsonMessageConverter" class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter"/>
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/views/"/>
<property name="suffix" value=".jsp"/>
</bean>
<mvc:interceptors>
<bean class="com.restfultest.interceptor.AutorizadorInterceptor"></bean>
<bean class="com.restfultest.interceptor.ConnectionInterceptor"></bean>
</mvc:interceptors>
<mvc:resources location="/resources/" mapping="/resources/**" />
Controller
public class SuggestionController {
#Autowired
private SuggestionService suggestionService;
#Autowired
private View view;
private static final String DATA_FIELD = "data";
private static final String ERROR_FIELD = "error";
private static final String CONTENT_RANGE_HEADER = "Content-Range";
private static final String ACCEPT_JSON = "Accept=application/json";
#RequestMapping(value = "/rest/suggestions/{suggestionId}", method = RequestMethod.GET, headers=ACCEPT_JSON)
public ModelAndView getSuggestion(#PathVariable("suggestionId") String suggestionId) {
Suggestion suggestion = null;
if (isEmpty(suggestionId) || suggestionId.length() < 5) {
String sMessage = "Error invoking getSuggestion - Invalid suggestion Id parameter";
return createErrorResponse(sMessage);
}
try {
suggestion = suggestionService.getSuggestionById(suggestionId);
} catch (Exception e) {
String sMessage = "Error invoking getSuggestion. [%1$s]";
return createErrorResponse(String.format(sMessage, e.toString()));
}
return new ModelAndView(view, DATA_FIELD, suggestion);
}
private ModelAndView createErrorResponse(String sMessage) {
return new ModelAndView(view, ERROR_FIELD, sMessage);
}
public static boolean isEmpty(String s_p) {
return (null == s_p) || s_p.trim().length() == 0;
}
The problem is your SuggestionController should have a #Controller annotation on it so Spring will treat it as a Controller (it should also be in the com.restfultest package).
Now I'm not sure if this matters butin the spring-context.xml where you have
<context:component-scan base-package="com.restfultest." />
I've never seen it with a dot at the end. So you might consider removing it, but it might not make a difference.
Related
So, I am pretty new to Spring MVC, and I think I'm having problems with configuration and I'm going crazy.
I've tried the solution here but either I'm unable to make it work or my mistake is something different
The error I'm getting is:
org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'servicioPersonalUrgenciasImplementacion': Unsatisfied dependency expressed through field 'dao'; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'com.modelo.dao.PersonalUrgenciasDAO' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {#org.springframework.beans.factory.annotation.Autowired(required=true)}
My components are:
Controller. in com.controladores package
#Controller
public class LoginController {
private ServicioPersonalUrgenciasLogin service;
#Autowired
public void setServicioPersonalUrgenciasLogin( ServicioPersonalUrgenciasLogin service) {
this.service = service;
}
#GetMapping("login")
public String login(Model model) {
PersonalUrgenciasLogin login = new PersonalUrgenciasLogin();
//Add customers to the model
model.addAttribute("login", login);
return "login";
}
...
}
Service interface and Service implementation, both in com.modelo.servicios
#Service
public interface ServicioPersonalUrgenciasLogin {
public boolean login(String username, String pwd) throws NoSuchAlgorithmException, UnsupportedEncodingException;
public void cambiarContrasenia(String username, String newpwd);
}
#Service
public class ServicioPersonalUrgenciasLoginImplementacion implements ServicioPersonalUrgenciasLogin {
#Autowired
private PersonalUrgenciasLoginDAO dao;
#Override
#Transactional
public boolean login(String username, String pwd) throws NoSuchAlgorithmException, UnsupportedEncodingException {
return dao.login(username, pwd);
}
#Override
#Transactional
public void cambiarContrasenia(String username, String newpwd) {
// I have to implement this,
}
}
DAO Interface and Implementation, both in com.modelo.dao package
I know this is not the way to implement a login, but I wanted something fast
public interface PersonalUrgenciasLoginDAO {
public boolean login(String username, String pwd) throws NoSuchAlgorithmException, UnsupportedEncodingException;
public void cambiarContrasenia(String username, String newpwd);
}
#Repository
public class PersonalUrgenciasLoginDAOImplementacion implements PersonalUrgenciasLoginDAO{
#Autowired
private SessionFactory factoria;
#Override
public boolean login(String username, String pwd) throws NoSuchAlgorithmException, UnsupportedEncodingException {
Session sesion = factoria.getCurrentSession();
MessageDigest md5 = MessageDigest.getInstance("MD5");
byte[] hashInOne = md5.digest(pwd.getBytes("UTF-8"));
String hashMD5String = getString(hashInOne);
Query<String> query2 = sesion.createQuery(
"SELECT usuario FROM PersonalUrgencias WHERE contrasenia=: pwd AND usuario =:user", String.class);
query2.setParameter("user", username);
query2.setParameter("pwd", hashMD5String);
List<String> haLogueado = query2.getResultList();
return !haLogueado.isEmpty();
}
#Override
public void cambiarContrasenia(String username, String newpwd) {
// TODO Auto-generated method stub
}
private static String getString( byte[] bytes )
{
StringBuffer sb = new StringBuffer();
for( int i=0; i<bytes.length; i++ )
{
byte b = bytes[ i ];
String hex = Integer.toHexString((int) 0x00FF & b);
if (hex.length() == 1)
{
sb.append("0");
}
sb.append( hex );
}
return sb.toString();
}
My Entity. I know it is not wired, but I don't want it to be
And then, my web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
id="WebApp_ID" version="3.1">
<display-name>triaje</display-name>
<absolute-ordering />
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
<servlet>
<servlet-name>dispatcher</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/applicationContext.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<servlet-mapping>
<servlet-name>dispatcher</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>
And this is my applicationContext.xml
I've tried just writting com in <context:component-scan...> too
<?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"
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/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd">
<!-- Add support for component scanning -->
<context:component-scan base-package="com, com.controladores, com.modelo.dao, com.modelo.entidades, com.modelo.servicios" />
<!-- Add support for conversion, formatting and validation support -->
<mvc:annotation-driven/>
<!-- Define Spring MVC view resolver -->
<bean
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/view/" />
<property name="suffix" value=".jsp" />
</bean>
<!-- Step 1: Define Database DataSource / connection pool -->
<bean id="myDataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"
destroy-method="close">
<property name="driverClass" value="com.mysql.cj.jdbc.Driver" />
<property name="jdbcUrl" value="jdbc:mysql://localhost:3316/trj?useSSL=false&serverTimezone=UTC" />
<property name="user" value="root" />
<property name="password" value="root" />
<!-- these are connection pool properties for C3P0 -->
<property name="minPoolSize" value="5" />
<property name="maxPoolSize" value="20" />
<property name="maxIdleTime" value="30000" />
</bean>
<!-- Step 2: Setup Hibernate session factory -->
<bean id="factoria"
class="org.springframework.orm.hibernate5.LocalSessionFactoryBean">
<property name="dataSource" ref="myDataSource" />
<property name="packagesToScan" value="com.modelo.entidades" />
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
<prop key="hibernate.show_sql">true</prop>
</props>
</property>
</bean>
<!-- Step 3: Setup Hibernate transaction manager -->
<bean id="myTransactionManager"
class="org.springframework.orm.hibernate5.HibernateTransactionManager">
<property name="sessionFactory" ref="factoria"/>
</bean>
<!-- Step 4: Enable configuration of transactional behavior based on annotations -->
<tx:annotation-driven transaction-manager="myTransactionManager" />
<!-- Add support for reading web resources: css, images, js... -->
<mvc:resources location="/resources/" mapping="/resources/**"></mvc:resources>
</beans>
I'm both grateful and sorry for anybody who has read through all of this
I am trying to do a restful service in spring mvc. This is my controller code,
#Controller
#RequestMapping("/manageuser")
public class ManageUserController {
#RequestMapping(value = "/deleteuser/{user}", method = RequestMethod.GET)
#ResponseStatus(value = HttpStatus.OK)
protected void deleteUser(#PathVariable String user, ModelMap model) {
System.out.println(user);
}
#RequestMapping(value = "/adduser/{user}", method = RequestMethod.GET)
#ResponseStatus(value = HttpStatus.OK)
protected void addUser(#PathVariable String user, ModelMap model) {
System.out.println(user);
}
}
My manageuser-servelt.xml
<context:component-scan base-package="com.example.web" use-default-filters="false"
>
<context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
</context:component-scan>
<mvc:annotation-driven />
<bean
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/" />
<property name="suffix" value=".jsp" />
</bean>
My web.xml content,
<servlet>
<servlet-name>manageuser</servlet-name>
<servlet-class>
org.springframework.web.servlet.DispatcherServlet
</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:manageuser-servlet.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>manageuser</servlet-name>
<url-pattern>/manageuser</url-pattern>
</servlet-mapping>
I am getting a 404 if I navigate to localhost/manageuser/adduser/Barney. What am I doing wrong here? Is this the right approach for RESTful services using spring-mvc?
Does "the System.out.println(user)" display the user ? If not, you have a problem in your mapping.
Check another Blog here : http://www.breathejava.com/restful-web-service-tutorial-spring-xml/.
I'm trying to start up Jetty with Spring MVC that contains jsp and #Controller. After start jetty I've got every time WARN if I try to call any pages. For example
ttp://localhost:8080/getMessageStats/
No mapping found for HTTP request with URI [/resources/css/bootstrap.min.css] in DispatcherServlet with name 'org.springframework.web.servlet.DispatcherServlet-57ad85ed' No mapping found for HTTP request with URI [/resources/js/bootstrap.min.js] in DispatcherServlet with name 'org.springframework.web.servlet.DispatcherServlet-57ad85ed'
web.xml
<web-app version="2.4"
xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
<display-name>Spring MVC Application</display-name>
<servlet>
<servlet-name>mvc-dispatcher</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/mvc-dispatcher-servlet.xml</param-value>
</context-param>
<servlet-mapping>
<servlet-name>mvc-dispatcher</servlet-name>
<url-pattern>/cloud/*</url-pattern>
</servlet-mapping>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
mvc-dispatcher-servlet.xml
<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: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/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd">
<context:component-scan base-package="net.cloud.web.statistics"/>
<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/pages/"/>
<property name="suffix" value=".jsp"/>
</bean>
<mvc:resources mapping="/resources/**" location="/resources/" />
<mvc:annotation-driven />
WebServer.class
public class WebServer {
private static final Logger log = LoggerFactory.getLogger(WebServer.class);
private static final String CONTEXT_PATH = "/";
private Server httpServer;
#Value("${http_statistics_port}")
private Integer port;
#Value("${start_page_path}")
private String startPagePath;
public void init() {
try {
httpServer = new Server(port);
httpServer.setHandler(getServletContextHandler());
httpServer.setStopAtShutdown(true);
httpServer.start();
log.info("WebServer start ...");
httpServer.join();
} catch (Exception ex) {
log.error("Exception in http server. Exception: {}", ex.getMessage());
}
}
private static ServletContextHandler getServletContextHandler() throws IOException {
WebAppContext contextHandler = new WebAppContext();
contextHandler.setErrorHandler(null);
contextHandler.setContextPath(CONTEXT_PATH);
WebApplicationContext context = getContext();
contextHandler.addServlet(new ServletHolder(new DispatcherServlet(context)), CONTEXT_PATH);
contextHandler.addEventListener(new ContextLoaderListener(context));
contextHandler.setResourceBase(new ClassPathResource("webapp").getURI().toString());
contextHandler.setDescriptor("/webapp/WEB-INF/web.xml");
return contextHandler;
}
private static WebApplicationContext getContext() {
AnnotationConfigWebApplicationContext context = new AnnotationConfigWebApplicationContext();
context.setConfigLocation("webapp/WEB-INF/");
log.info("CONTEXT name {} locations {}", context.getDisplayName(), context.getConfigLocations());
return context;
}
}
GetMessagesStatsController.class
#Controller
#RequestMapping("/getMessageStats")
public class GetMessageStatsController {
#RequestMapping(value = "/", method = RequestMethod.GET)
public String printWelcome(ModelMap model) {
GetMessageStatsRPC rpc = new GetMessageStatsRPC();
model.addAttribute("result", rpc.getResult());
return "getMessageStats";
}
#RequestMapping(value = "/json", method = RequestMethod.GET)
public #ResponseBody
String generateJsonResponse() {
GetMessageStatsRPC rpc = new GetMessageStatsRPC();
return rpc.getResult().toString();
}
}
Project tree (Sorry, no rating to upload picture). Classes:
src/java/main/net/.../WebServer.class;src/java/main/net/.../GetMessagesStatsController.class
src/main/webapp/WEB-INF/web.xml; src/main/webapp/WEB-INF/mvc-dispatcher-servlet.xml; src/main/webapp/WEB-INF/pages/*.jsp; src/main/webapp/resources/css and src/main/webapp/resources/js
Guys, any idea?
First when the application is deployed it checks the web.xml and gets
<url-pattern>/*</url-pattern>
Then it initializes the DispatcherServlet and initializes the initialization paramerters using
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/mvc-dispatcher-servlet.xml</param-value>
</context-param>
(WEB-INF must start with /)
then in mvc-dispatcher-servlet.xml the view resolver resolves the rendering pages.
Change <property name="prefix" value="WEB-INF/pages/"/>
to
<property name="prefix" value="/WEB-INF/pages/"/>
Finally in your controller #RequestMapping("/getMessageStats")
so finally launching the application as
http://localhost:8080/ProjectName/getMessageStats/
invokes printWelcome() of GetMessagesStatsController and
http://localhost:8080/ProjectName/getMessageStats/json
invokes generateJsonResponse()
Looks like the viewResolver has problem. change the prefix to /WEB-INF/pages/, as follow:
<bean id="viewResolver" class="org.springframework.web.servlet.view.UrlBasedViewResolver">
<property name="viewClass" value="org.springframework.web.servlet.view.JstlView" />
<property name="prefix" value="/WEB-INF/pages/"/>
<property name="suffix" value=".jsp"/>
</bean>
Without the slash it means a relative path, so it will find the page at relativePath(getMessageStats/) + prefix(WEB-INF/pages/) + request(getMessageStats) +suffix(.jsp).
I have a really strange Problem with my new Vaadin Project.
If I start my Application and log in with a User, then switch the Browser and open the Site again, it seems that the Sessions will be shared. I have the same Content in both Windows.
I readed a few forum Posts about this Problem, all they say don´t open the App in the same Browser. Ok, i startet a VM, but, the same Problem. Or, remove all static variables from the context. I have done that, now using the ThreadLocal-Pattern.
I have absolutly no idea whats wrong. What I believe is that the Spring Context kills Vaadin, has anyone ever had this experience with Vaadin and Spring?
To bring Spring and Vaadin togehter I use the Spring Intgration Addon for Vaadin by Nicolas Frankel (https://vaadin.com/directory#addon/spring-integration)
I will post my Application Code for further investigation.
===================
The Vaadin Application Class:
public class MainApplication extends Application implements HttpServletRequestListener {
private static final long serialVersionUID = 2067470624065324231L;
private static ThreadLocal<MainApplication> threadLocal = new ThreadLocal<MainApplication>();
private Window mainWindow;
//Viewmanager
private ViewManager viewManager;
private Professor professor;
//DAO Classes and Bean Setter´s
ProfessorDAO professorDAO;
QuestionDAO questionDAO;
AnswerDAO answerDAO;
AnsweredDAO answeredDAO;
QRCodeDAO qrCodeDAO;
public void setProfessorDAO(ProfessorDAO professorDAO) {
System.out.println("Setting ProfessorDAO!");
System.out.println(professorDAO.toString());
this.professorDAO = professorDAO;
}
public void setQuestionDAO(QuestionDAO questionDAO) {
System.out.println("Setting QuestionDAO!");
System.out.println(questionDAO.toString());
this.questionDAO = questionDAO;
}
public void setAnswerDAO(AnswerDAO answerDAO) {
System.out.println("Setting AnswerDAO!");
System.out.println(answerDAO.toString());
this.answerDAO = answerDAO;
}
public void setAnsweredDAO(AnsweredDAO answeredDAO) {
System.out.println("Setting AnsweredDAO!");
System.out.println(answeredDAO.toString());
this.answeredDAO = answeredDAO;
}
public void setQrCodeDAO(QRCodeDAO qrCodeDAO) {
System.out.println("Setting QRCodeDAO!");
System.out.println(qrCodeDAO.toString());
this.qrCodeDAO = qrCodeDAO;
}
public ProfessorDAO getProfessorDAO() {
return professorDAO;
}
public QuestionDAO getQuestionDAO() {
return questionDAO;
}
public AnswerDAO getAnswerDAO() {
return answerDAO;
}
public AnsweredDAO getAnsweredDAO() {
return answeredDAO;
}
public QRCodeDAO getQrCodeDAO() {
return qrCodeDAO;
}
//Currently logged in Professor.
public Professor getProfessor() {
return professor;
}
public void setProfessor(Professor professor) {
this.professor = professor;
}
public static void setInstance(MainApplication application){
threadLocal.set(application);
}
public ViewManager getViewManager() {
return viewManager;
}
public void setViewManager(ViewManager viewManager) {
this.viewManager = viewManager;
}
public static MainApplication getInstance(){
return threadLocal.get();
}
public MainApplication() {
}
#Override
public void init() {
setInstance(this);
setTheme("crs");
mainWindow = new Window("CRS -- Classroom Response System");
setMainWindow(mainWindow);
viewManager = new ViewManager(mainWindow);
viewManager.switchScreen(LoginScreen.class.getName(), new LoginScreen());
//mainWindow = new MainWindow("CRS -- Classroom Response System");
//setMainWindow(mainWindow);
}
public void onRequestStart(HttpServletRequest request,
HttpServletResponse response) {
MainApplication.setInstance(this);
}
public void onRequestEnd(HttpServletRequest request,
HttpServletResponse response) {
threadLocal.remove();
}
The web.xml:
<display-name>CRSServer</display-name>
<context-param>
<description>
Vaadin production mode</description>
<param-name>productionMode</param-name>
<param-value>false</param-value>
</context-param>
<servlet>
<servlet-name>CRS</servlet-name>
<servlet-class>ch.frankel.vaadin.spring.SpringApplicationServlet</servlet-class>
<init-param>
<description>
Vaadin application class to start</description>
<param-name>applicationBeanName</param-name>
<param-value>cs.hm.edu.kreipl.crs.frontend.MainApplication</param-value>
</init-param>
<init-param>
<description>
Application widgetset</description>
<param-name>widgetset</param-name>
<param-value>cs.hm.edu.kreipl.crs.widgetset.CrsserverWidgetset</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>CRS</servlet-name>
<url-pattern>/*</url-pattern>
</servlet-mapping>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<listener>
<listener-class>org.springframework.web.context.request.RequestContextListener</listener-class>
</listener>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>WEB-INF/spring-config.xml</param-value>
</context-param>
The Spring Context
<context:component-scan base-package="cs.hm.edu.kreipl.crs" />
<bean id="myDataSource"
class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName"
value="com.mysql.jdbc.Driver">
</property>
<property name="url" value="jdbc:mysql://localhost:3306/crs"></property>
<property name="username" value="crs"></property>
<property name="password" value="password"></property>
</bean>
<bean id="mySessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
<property name="dataSource" ref="myDataSource"/>
<property name="annotatedClasses">
<list>
<value>cs.hm.edu.kreipl.crs.database.entity.Professor</value>
<value>cs.hm.edu.kreipl.crs.database.entity.Answer</value>
<value>cs.hm.edu.kreipl.crs.database.entity.Answered</value>
<value>cs.hm.edu.kreipl.crs.database.entity.Question</value>
<value>cs.hm.edu.kreipl.crs.database.entity.QuestionQRCode</value>
</list>
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.MySQL5Dialect</prop>
<prop key="hibernate.show_sql">true</prop>
<prop key="hbm2ddl.auto">create</prop>
</props>
</property>
</bean>
<bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory" ref="mySessionFactory" />
</bean>
<tx:annotation-driven/>
<!-- VAADIn Startup -->
<bean id="cs.hm.edu.kreipl.crs.frontend.MainApplication" class="cs.hm.edu.kreipl.crs.frontend.MainApplication">
<property name="answerDAO" ref="answerDAO" />
<property name="answeredDAO" ref="answeredDAO" />
<property name="professorDAO" ref="professorDAO" />
<property name="qrCodeDAO" ref="qrCodeDAO" />
<property name="questionDAO" ref="questionDAO" />
</bean>
<bean id="professorDAO" class="cs.hm.edu.kreipl.crs.database.implementation.ProfessorDAOImpl">
<constructor-arg>
<value>cs.hm.edu.kreipl.crs.database.entity.Professor</value>
</constructor-arg>
<property name="hibernateTemplate" ref="mySessionFactory"/>
</bean>
<bean id="questionDAO" class="cs.hm.edu.kreipl.crs.database.implementation.QuestionDAOImpl">
<constructor-arg>
<value>cs.hm.edu.kreipl.crs.database.entity.Question</value>
</constructor-arg>
<property name="hibernateTemplate" ref="mySessionFactory"/>
</bean>
<bean id="qrCodeDAO" class="cs.hm.edu.kreipl.crs.database.implementation.QRCodeDAOImpl">
<constructor-arg>
<value>cs.hm.edu.kreipl.crs.database.entity.QuestionQRCode</value>
</constructor-arg>
<property name="hibernateTemplate" ref="mySessionFactory"/>
</bean>
<bean id="answerDAO" class="cs.hm.edu.kreipl.crs.database.implementation.AnswerDAOImpl">
<constructor-arg>
<value>cs.hm.edu.kreipl.crs.database.entity.Answer</value>
</constructor-arg>
<property name="hibernateTemplate" ref="mySessionFactory"/>
</bean>
<bean id="answeredDAO" class="cs.hm.edu.kreipl.crs.database.implementation.AnsweredDAOImpl">
<constructor-arg>
<value>cs.hm.edu.kreipl.crs.database.entity.Answered</value>
</constructor-arg>
<property name="hibernateTemplate" ref="mySessionFactory"/>
</bean>
<bean id="deviceDAO" class="cs.hm.edu.kreipl.crs.database.implementation.DeviceDAOImpl">
<constructor-arg>
<value>cs.hm.edu.kreipl.crs.database.entity.Device</value>
</constructor-arg>
<property name="hibernateTemplate" ref="mySessionFactory"/>
</bean>
If you need any further classes please let me now.
I am using Vaadin + Spring but without Spring Integration Addon.
I think that the problem is that you are not creating a new Vaadin applicationBean for the new user that requests the application.
In my app, I pass the applicationBean in web.xml. My Vaadin application is annotated with:
Application Class
#Component(value = "appName")
#Scope(value = "session")
class AppName extends Application {
....
}
WEB.XML
<init-param>
<param-name>applicationBean</param-name>
<param-value>appName</param-value>
</init-param>
And for every new session Servlet returns new app.
In your web.xml you pass your vaadin application like this:
Your WEB.XML
<init-param>
<description>
Vaadin application class to start</description>
<param-name>applicationBeanName</param-name>
<param-value>cs.hm.edu.kreipl.crs.frontend.MainApplication</param-value>
</init-param>
So it returns every time the same app for all users. And that's what I think causes the problem.
I'm having trouble doing a Spring (using 3.0.5.RELEASE) mapping. I want to map the URL http://mydomain/context-path/user/registrationform.jsp to my JSP page at
/WEB-INF/views/user/registrationform.jsp
but I'm getting a 404. I have my controller setup like so …
#Controller
#RequestMapping("registrationform.jsp")
public class RegistrationController {
private static Logger LOG = Logger.getLogger(RegistrationController.class);
…
public void setRegistrationValidation(
RegistrationValidation registrationValidation) {
this.registrationValidation = registrationValidation;
}
// Display the form on the get request
#RequestMapping(method = RequestMethod.GET)
public String showRegistration(Map model) {
final Registration registration = new Registration();
model.put("registration", registration);
return "user/registrationform";
}
here is my dispatcher-servlet.xml …
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
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/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">
<!-- Enable annotation driven controllers, validation etc... -->
<mvc:annotation-driven />
<context:component-scan base-package="com.burrobuie.eventmaven" />
<bean id="viewResolver"
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix">
<value>/WEB-INF/views/</value>
</property>
<property name="suffix">
<value>.jsp</value>
</property>
</bean>
<bean id="messageSource"
class="org.springframework.context.support.ReloadableResourceBundleMessageSource">
<property name="basename" value="/messages" />
</bean>
</beans>
and here is my web.xml …
<servlet>
<servlet-name>dispatcher</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>dispatcher</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
What else do I need to configure (or configure differently) to make this mapping work? This is harder than - Dave
#Controller
#RequestMapping("registrationform.jsp")
public class RegistrationController {
The RequestMapping annotation at class level should be use for a common url pattern like "item/*" and all the links that contains "/item" followed by other pattern would be mapped it to the controller. "user/" in your case
The RequestMapping annotation at method level is used for mapping the sub URL like "item/add" or "item/delete", "registrationform.jsp' in your case
So try this:
#Controller
#RequestMapping("/user")
public class RegistrationController {
private static Logger LOG = Logger.getLogger(RegistrationController.class);
…
public void setRegistrationValidation(
RegistrationValidation registrationValidation) {
this.registrationValidation = registrationValidation;
}
// Display the form on the get request
#RequestMapping(value="/registrationform.jsp",method = RequestMethod.GET)
public String showRegistration(Map model) {
final Registration registration = new Registration();
model.put("registration", registration);
return "user/registrationform";
}
This will map /user/registrationform.jsp
Change the RequestMapping to:
#RequestMapping("/user/registrationform.jsp")